<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>

<body>
  <!-- 面包屑导航 -->
  <div>
    <span class="layui-breadcrumb">
      <a href="/dashboard">仪表盘</a>
      <a><cite>商品管理</cite></a>
    </span>
  </div>
  <!-- /面包屑导航 -->
  <a class="layui-btn layui-btn-warm" href="/itemAdd" style="margin-top: 20px">增加商品</a>
  <table class="layui-table">
    <thead>
      <tr>
        <th>商品名称</th>
        <th>商品价格</th>
        <th>商品库存</th>
        <th>商品描述</th>
        <th>商品封面</th>
        <th>商品状态</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <% for(var i=0;i<items.length;i++) { %>
        <tr>
          <td>
            <%= items[i].itemName %>
          </td>
          <td>
            <%= items[i].itemPrice %>
          </td>
          <td>
            <%= items[i].itemStore %>
          </td>
          <td title="<%= items[i].itemDesc %>">
            <%= items[i].itemDesc? items[i].itemDesc.length>10?
              items[i].itemDesc.substr(0,10) + '...'
              :items[i].itemDesc : '暂无描述' %>
          </td>
          <td>
            <img width="100"
              src="<%= items[i].itemThumb? items[i].itemThumb : 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2F11712326.s21i-11.faiusr.com%2F2%2FABUIABACGAAgpfm_wQUohqX7ugEw2AQ4kAM.jpg' %>"
              alt="">
          </td>
          <td>
            <%= items[i].isSale==='0' ? '上架' : '下架' %>
          </td>
          <td>
            <a href="/itemUpdate?id=<%= items[i]._id %>" class="layui-btn layui-btn-primary layui-btn-xs">编辑</a>
            <button class="layui-btn del layui-btn-danger layui-btn-xs" _id="<%= items[i]._id %>">删除</button>
          </td>
        </tr>
        <% } %>

    </tbody>
  </table>
  <div id="page"></div>
  <script src="/static/layui/layui.js"></script>
  <script>
    layui.use(['laypage', 'jquery', 'layer'], function () {
      var laypage = layui.laypage;
      var $ = layui.jquery;
      var layer = layui.layer;

      $(".del").click(function () {
        // 确认框
        var _this = this;
        layer.confirm('您确认删除嘛?', { icon: 3, title: '删除商品' }, function (index) {
          $.ajax({
            url: '/delItem',
            data: {
              id: $(_this).attr("_id")
            },
            success: function (res) {
              if (res.code === 0) {
                layer.msg(res.msg, {
                  icon: 1,
                  time: 1000
                }, function () {
                  // 成功后刷新页面
                  history.go(0)
                });
              } else {
                layer.msg(res.msg, {
                  icon: 2,
                  time: 1000
                });
              }
            }
          })
          layer.close(index);
        });
      })
      //创建分页组件
      laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        , count: <%= count %> //数据总数，从服务端得到
        , limit: 2
        , curr: <%= page %>
        , jump: function (obj, first) {

        //首次不执行
        if (!first) {
          location.href = '/itemList?page=' + obj.curr + "&pageSize=" + obj.limit;
        }
      }
      });
    })
  </script>
</body>

</html>